/* @file: led.S
 * @info: 汇编流水灯
 */
 
 /*
 * on: 0, off: 1
 * LED1 -> D22 -> GPJ0_3
 * LED2 -> D23 -> GPJ0_4
 * LED3 -> D24 -> GPJ0_5
 * LED4 -> D25 -> PWMTOUT1/GPD0_1
 */
#define PS_HOLD_CONTROL     0xE010E81C

#define GPJ0CON             0xE0200240
#define GPJ0DAT             0xE0200244

#define GPD0CON             0xE02000A0
#define GPD0DAT             0xE02000A4

.global _start
_start:
    /* 给5V电源置锁 */
    ldr r0, = PS_HOLD_CONTROL
    ldr r1, [r0]
    orr r1, r1, #0x300
    orr r1, r1, #0x1
    str r1, [r0]

led_init:
    /* 初始化LED */
    ldr r0, = 0x00111000
    ldr r1, = GPJ0CON
    str r0, [r1]

    ldr r0, = 0x00000010
    ldr r1, = GPD0CON
    str r0, [r1]

led_run:
    /* 点亮LED1 */
    ldr r0, = ~(0x1<<3)
    ldr r1, = GPJ0DAT
    str r0, [r1]
    bl delay

    /* 点亮LED2 */
    ldr r0, = ~(0x1<<4)
    ldr r1, = GPJ0DAT
    str r0, [r1]
    bl delay

    /* 点亮LED3 */
    ldr r0, = ~(0x1<<5)
    ldr r1, = GPJ0DAT
    str r0, [r1]
    bl delay

    /* 点亮LED4 */
    ldr r0, = ((0x1<<3)|(0x1<<4)|(0x1<<5))
    ldr r1, = GPJ0DAT
    str r0, [r1]
    ldr r0, = ~(0x1<<1)
    ldr r1, = GPD0DAT
    str r0, [r1]
    bl delay

    /* 熄灭LED4 */
    ldr r0, = (0x1<<1)
    ldr r1, = GPD0DAT
    str r0, [r1]

    b led_run

// 延时函数
delay:
    ldr r2, = 9000000
    ldr r3, = 0x0
delay_loop:
    sub r2, r2, #1              //r2 = r2 -1
    cmp r2, r3
    bne delay_loop
    mov pc, lr                  //实现程序跳转
